package controller

import (
	"github.com/cn-ygf/go-admin/internal/action"
	"github.com/cn-ygf/go-admin/internal/modular/system/service"
	"github.com/cn-ygf/go-admin/internal/modular/system/vo"
	"github.com/cn-ygf/go-admin/pkg/utils"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
)

// PostController 岗位管理
type PostController struct {
	Ctx iris.Context
}

// GetList 查询岗位列表
func (c *PostController) GetList() mvc.Result {
	if !hasPermi(c.Ctx, "system:post:list") {
		return nil
	}
	var (
		pageNum  int
		pageSize int
		err      error
	)
	pageNum, err = c.Ctx.URLParamInt("pageNum")
	if err != nil {
		pageNum = 1
	}
	pageSize, err = c.Ctx.URLParamInt("pageSize")
	if err != nil {
		pageSize = 10
	}
	if pageSize > 1000 {
		pageSize = 1000
	}
	list, total, err := service.PostService.QueryList(&vo.QueryPostListReq{
		PostName: c.Ctx.URLParam("postName"),
		PostCode: c.Ctx.URLParam("postCode"),
		Status:   c.Ctx.URLParam("status"),
		PageNum:  pageNum,
		PageSize: pageSize,
	})
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.ServerError()
	}
	return action.Data(map[string]any{
		"rows":  list,
		"total": total,
	})
}

// GetBy 根据id获取岗位信息
func (c *PostController) GetBy(id int64) mvc.Result {
	if !hasPermi(c.Ctx, "system:post:query") {
		return nil
	}
	post, err := service.PostService.GetByID(id)
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.ServerError()
	}
	return action.Data(post)
}

// Put 修改岗位
func (c *PostController) Put(req vo.EditPostReq) mvc.Result {
	if !hasPermi(c.Ctx, "system:post:edit") {
		return nil
	}
	if !service.PostService.CheckPostNameUnique(req.PostId, req.PostName) {
		return action.Error("修改岗位'" + req.PostName + "'失败，岗位名称已存在")
	}
	if !service.PostService.CheckPostCodeUnique(req.PostId, req.PostCode) {
		return action.Error("修改岗位'" + req.PostName + "'失败，岗位编码已存在")
	}
	req.UpdateBy = getCurrentUser(c.Ctx).UserName
	err := service.PostService.Update(&req)
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.Error(err.Error())
	}
	return action.OK()
}

// Post 添加岗位
func (c *PostController) Post(req vo.AddPostReq) mvc.Result {
	if !hasPermi(c.Ctx, "system:post:add") {
		return nil
	}
	if !service.PostService.CheckPostNameUnique(0, req.PostName) {
		return action.Error("新增岗位'" + req.PostName + "'失败，岗位名称已存在")
	}
	if !service.PostService.CheckPostCodeUnique(0, req.PostCode) {
		return action.Error("新增岗位'" + req.PostName + "'失败，岗位编码已存在")
	}
	req.CreateBy = getCurrentUser(c.Ctx).UserName
	err := service.PostService.Add(&req)
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.Error(err.Error())
	}
	return action.OK()
}

// DeleteBy 删除岗位
func (c *PostController) DeleteBy(idStr string) mvc.Result {
	if !hasPermi(c.Ctx, "system:post:remove") {
		return nil
	}
	ids := utils.IdsStrToInt64(idStr)
	if len(ids) < 1 {
		return action.ParamError()
	}

	err := service.PostService.Delete(ids)
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.Error(err.Error())
	}
	return action.OK()
}

// PostExport 导出岗位
func (c *PostController) PostExport() mvc.Result {
	if !hasPermi(c.Ctx, "system:post:export") {
		return action.RawError()
	}
	postList, err := service.PostService.All()
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.RawError()
	}
	titleRow := []string{"岗位编号", "岗位编码", "岗位名称", "状态", "创建时间", "备注"}

	statusMaps, err := service.DictDataService.GetDictMaps("sys_normal_disable")
	if err != nil {
		systemLog.Errorln(err.Error())
		return action.RawError()
	}
	data := make([][]any, len(postList))
	for index, item := range postList {
		row := make([]any, 6)
		row[0] = item.PostID
		row[1] = item.PostCode
		row[2] = item.PostName
		row[3] = statusMaps[item.Status]
		row[4] = utils.UnixTimeToString(item.CreatedAt)
		row[5] = item.Remark
		data[index] = row
	}
	return action.ExportExcel(data, titleRow)
}
